﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;

namespace Web.Page
{
    public partial class CS_WLSXZ_Query : BasePage
    {
        protected string wllxbh;
        protected string wlsxbh;
        CS_WLLX_Dao daoWLLX = new CS_WLLX_Dao();
        CS_WLLXSX_Dao daoWLLXSX = new CS_WLLXSX_Dao();
        CS_WLSXZ_Dao daoWLSXZ = new CS_WLSXZ_Dao();

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            wllxbh = Request.QueryString["wllxbh"];
            wlsxbh = Request.QueryString["wlsxbh"];

            if (!IsPostBack)
            {
                string vpath = Request.QueryString[WLTree.VPATH];
                if (vpath != null)
                {
                    Cache.Insert(WLTree.VPATH, vpath);
                }

                this.lbTitle.Text = "物料属性值列表";
                BindGridview();
            }
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            BindGridview();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindGridview();
            SavePageIndex(e.NewPageIndex);
        }

        private void BindGridview()
        {
            GridView1.PageIndex = RestorePageIndex();

            List<CS_WLLX> listWLLX = daoWLLX.QueryAll();
            List<CS_WLLXSX> listWLLXSX = daoWLLXSX.QueryAll();
            List<CS_WLSXZ> listCS_WLSXZ;

            if (wllxbh != null && wlsxbh != null)
            {
                listCS_WLSXZ = daoWLSXZ.Query(wllxbh, wlsxbh);
            }
            else
            {
                listCS_WLSXZ = daoWLSXZ.QueryAll();
            }

            List<CS_WLSXZ_Ex> listEx = new List<CS_WLSXZ_Ex>();
            CS_WLSXZ_Ex ce;
            foreach (CS_WLSXZ c in listCS_WLSXZ)
            {
                ce = new CS_WLSXZ_Ex();
                ce.MC = c.MC;
                ce.BH = c.BH;
                ce.WLLXBH = c.WLLXBH;
                ce.WLSXBH = c.WLSXBH;

                CS_WLLX cpl = listWLLX.Find(delegate(CS_WLLX p) { return p.BH == c.WLLXBH; });// daoWLLX.Query(c.WLLXBH);
                ce.WLLXMC = cpl == null ? "" : cpl.MC;
                CS_WLLXSX cpm = listWLLXSX.Find(delegate(CS_WLLXSX p) { return p.BH == c.WLSXBH && p.WLLXBH == c.WLLXBH; });// daoWLLXSX.Query(c.WLLXBH, c.WLSXBH);
                ce.WLSXMC = cpm == null ? "" : cpm.MC;
                listEx.Add(ce);
            }


            this.GridView1.DataSource = listEx;
            this.GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    ((ImageButton)(e.Row.Cells[7].Controls[0])).Attributes.Add("onclick ", "return   confirm( '确定删除？ ') ");
            //}
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string bh = GridView1.DataKeys[e.RowIndex].Value.ToString();
            string wllxbh = GridView1.Rows[e.RowIndex].Cells[2].Text;
            string wlsxbh = GridView1.Rows[e.RowIndex].Cells[4].Text;
            int result = daoWLSXZ.Delete(wllxbh, wlsxbh, bh);
            if (result == 1)
            {
                lbResult.Text = "删除成功";
                BindGridview();
                Cache.Insert(WLTree.LEVEL, WLTree.WLSXZ);

                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "onSuccess", "<script type='text/javascript'>parent.refresh('tree');</script>", false);
            }
        }

        protected class CS_WLSXZ_Ex
        {
            public string WLLXBH { get; set; }
            public string WLLXMC { get; set; }
            public string WLSXBH { get; set; }
            public string WLSXMC { get; set; }
            public string BH { get; set; }
            public string MC { get; set; }
        }

        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            if (wllxbh != null && wlsxbh != null)
            {
                Response.Redirect(string.Format("CS_WLSXZ_Add.aspx?wllxbh={0}&wlsxbh={1}", wllxbh, wlsxbh));
            }
            else
            {
                Response.Redirect("CS_WLSXZ_Add.aspx");
            }
        }
    }
}